package com.iaz.tech.tools.ai.neuron.exec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iaz.tech.tools.ai.neuron.runtime.INeuronRuntimeAbility; 
import com.iaz.tech.tools.common.utils.UtilTools; 
import com.iaz.tech.tools.property.impl.PROPERTYRIGHT;
import com.iaz.tech.tools.property.project.Project;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.relationship.Relationship;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronClassStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronFunctionStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronParameterStruct;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 神经元类代码生成器
 */
@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronClassCodeGenerator extends NeuronCodeGenerator {
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronPackageCodeGenerator.class);
    
    public NeuronClassCodeGenerator() {
        super();
    }

    public NeuronClassCodeGenerator(INeuronRuntimeAbility runtime) {
        super();
        this.setRuntime(runtime);
    }
    
    @Override
    public Relationship toRelationship(IUMLNeuronAble uml) throws Exception {
        throw new IllegalArgumentException("uml必须为Class");
    }

    /**
     * 生成Class 的对象类
     */
    @Override
    public String generateSourceCode(Project project, IUMLNeuronAble uml) {
        String result = "";
        try {
            if (uml == null) {
                throw new IllegalArgumentException("uml不能为null");
            }
            if (!(uml instanceof NeuronClassStruct)){
                throw new IllegalArgumentException("uml必须为Class");
            }
            if (this.getRuntime() == null){
                throw new IllegalArgumentException("runtime不能为null");
            }
            NeuronClassStruct clazz = (NeuronClassStruct) uml;
            if (UtilTools.IsNull(clazz.getName())) {
                throw new IllegalArgumentException("Class名称不能为null");
            }
            StringBuffer sBuf = new StringBuffer();

            sBuf.append(this.generatorPropertyRight(clazz.getRight()));
            sBuf.append(" ");
            sBuf.append("class ");
            sBuf.append(clazz.getName());

            /*
             * 类或者接口
             */
            

            sBuf.append(" {");
            sBuf.append("\n");

            if (clazz.getProperties() != null ) {
                for (NeuronParameterStruct prop : clazz.getProperties()) {
                    if (UtilTools.IsNull(prop.getName())) {
                        throw new IllegalArgumentException("属性名称不能为null");
                    }
                    sBuf.append(this.generatorPropertyRight(prop.getRight()));
                    sBuf.append(" ");
                    
                    sBuf.append(this.generatorPropertyType(prop.isSignAbstract(), prop.isSignFinal(), prop.isSignStatic()));
                    sBuf.append(" ");

                    sBuf.append(prop.getDataType());
                    sBuf.append(" ");
                    sBuf.append(prop.getName());

                    if (!UtilTools.IsNull(prop.getDefaultValue())) {
                        sBuf.append(" = ");
                        sBuf.append(prop.getDefaultValue());
                    }
                    sBuf.append(";");
                    sBuf.append("\n");
                }
            }

            if (clazz.getFunctions()  != null ) {
                for (NeuronFunctionStruct method : clazz.getFunctions()) {
                    if (UtilTools.IsNull(method.getName())) {
                        throw new IllegalArgumentException("方法名称不能为null");
                    }
                    sBuf.append(this.generatorPropertyRight(method.getRight()));
                    sBuf.append(" ");
                    
                    sBuf.append(this.generatorPropertyType(method.isSignAbstract(), method.isSignFinal(), method.isSignStatic()));
                    sBuf.append(" ");
                    sBuf.append(method.getDataType());
                    sBuf.append(" ");
                    sBuf.append(method.getName());

                    sBuf.append("(");
                    if (method.getParameters() != null) {
                        int index = 0;
                        for (NeuronParameterStruct param : method.getParameters()) {
                            if (index > 0) {
                                sBuf.append(",");
                            }
                            sBuf.append(param.getDataType());
                            sBuf.append(" ");
                            sBuf.append(param.getName());
                            index++;
                        }
                    }
                    sBuf.append(")");



                    sBuf.append(";");
                    sBuf.append("\n");
                }
            }

            sBuf.append("}");
            result = sBuf.toString();
        } catch(Exception e) {
            logger.error("生成Class代码失败", e);
            result = "";
        }
        return result;
    }

}
